// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#pragma once

#include <memory>

#include <nebula/array/array_decimal.h>
#include <nebula/array/builder_base.h>
#include <nebula/array/builder_binary.h>
#include <nebula/array/data.h>
#include <turbo/utility/status.h>
#include <nebula/types/type.h>
#include <turbo/base/macros.h>

namespace nebula {

    /// \addtogroup numeric-builders
    ///
    /// @{

    class TURBO_EXPORT Decimal128Builder : public FixedSizeBinaryBuilder {
    public:
        using TypeClass = Decimal128Type;
        using value_type = Decimal128;

        explicit Decimal128Builder(const std::shared_ptr<DataType> &type,
                                   MemoryPool *pool = default_memory_pool(),
                                   int64_t alignment = kDefaultBufferAlignment);

        using FixedSizeBinaryBuilder::append;
        using FixedSizeBinaryBuilder::append_values;
        using FixedSizeBinaryBuilder::reset;

        turbo::Status append(Decimal128 val);

        void unsafe_append(Decimal128 val);

        void unsafe_append(std::string_view val);

        turbo::Status finish_internal(std::shared_ptr<ArrayData> *out) override;

        /// \cond FALSE
        using ArrayBuilder::finish;
        /// \endcond

        turbo::Status finish(std::shared_ptr<Decimal128Array> *out) { return finish_typed(out); }

        std::shared_ptr<DataType> type() const override { return decimal_type_; }

    protected:
        std::shared_ptr<Decimal128Type> decimal_type_;
    };

    class TURBO_EXPORT Decimal256Builder : public FixedSizeBinaryBuilder {
    public:
        using TypeClass = Decimal256Type;
        using value_type = Decimal256;

        explicit Decimal256Builder(const std::shared_ptr<DataType> &type,
                                   MemoryPool *pool = default_memory_pool(),
                                   int64_t alignment = kDefaultBufferAlignment);

        using FixedSizeBinaryBuilder::append;
        using FixedSizeBinaryBuilder::append_values;
        using FixedSizeBinaryBuilder::reset;

        turbo::Status append(const Decimal256 &val);

        void unsafe_append(const Decimal256 &val);

        void unsafe_append(std::string_view val);

        turbo::Status finish_internal(std::shared_ptr<ArrayData> *out) override;

        /// \cond FALSE
        using ArrayBuilder::finish;
        /// \endcond

        turbo::Status finish(std::shared_ptr<Decimal256Array> *out) { return finish_typed(out); }

        std::shared_ptr<DataType> type() const override { return decimal_type_; }

    protected:
        std::shared_ptr<Decimal256Type> decimal_type_;
    };

    using DecimalBuilder = Decimal128Builder;

    /// @}

}  // namespace nebula
